from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *

# import resources

title="&CopyPasta"

import doAbout

class MainPlugin(object):
 def __init__(self, iface):
   # Save a reference to the QGIS iface
   self.iface = iface

 def initGui(self):

   self.aMapExtent = QAction("Map Extent", self.iface.mainWindow())
   QObject.connect(self.aMapExtent,SIGNAL("triggered()"),self.dMapExtent)

   self.aMapExtentSQL = QAction("Map Extent SQL", self.iface.mainWindow())
   QObject.connect(self.aMapExtentSQL,SIGNAL("triggered()"),self.dMapExtentSQL)

   self.aProjectFile = QAction("Project File Path", self.iface.mainWindow())
   QObject.connect(self.aProjectFile,SIGNAL("triggered()"),self.dProjectFile)

   self.aLayerInfo = QAction("Layer Info", self.iface.mainWindow())
   QObject.connect(self.aLayerInfo,SIGNAL("triggered()"),self.dLayerInfo)

   self.about = QAction("About CopyPasta",self.iface.mainWindow())
   QObject.connect(self.about,SIGNAL("triggered()"),self.clickAbout)

   self.iface.addPluginToMenu(title,self.aMapExtent)
   self.iface.addPluginToMenu(title,self.aMapExtentSQL)
   self.iface.addPluginToMenu(title,self.aProjectFile)
   self.iface.addPluginToMenu(title,self.aLayerInfo)
   self.iface.addPluginToMenu(title,self.about)

#    self.menu.addSeparator()

 def dMapExtent(self):
   text = self.iface.mapCanvas().extent().toString()
   self.doPaste(text)

 def dMapExtentSQL(self):
   text = self.iface.mapCanvas().extent().toString()
   # 13.5702225179249876,41.2134192420407501 : 13.5768356834183166,41.2182110366311107
   text1 = text.replace(" : ","),ST_Point(")
# ST_Intersects(the_geom, ST_SetSRID(ST_MakeBox2D(ST_Point(,),ST_Point(,)),4326)) = TRUE
   text = "ST_Intersects(the_geom, ST_SetSRID(ST_MakeBox2D(ST_Point(" + text1 + ")),4326)) = TRUE"
   self.doPaste(text)


 def dProjectFile(self):
   text = QgsProject.instance().fileName()
   self.doPaste(text)

 def dLayerInfo(self):
   info = []
   for iLayer in range(self.iface.mapCanvas().layerCount()):
     layer = self.iface.mapCanvas().layer(iLayer)
     if layer.type() == layer.RasterLayer:
       ts = "Raster"
     elif layer.type() == layer.VectorLayer:
       ts = "Vector"
     else:
       ts = "Unknown"
     info.append(",".join([str(iLayer),str(layer.name()),ts,str(layer.source())]))
   self.doPaste("\n".join(info))

 def clickAbout(self):
   d = doAbout.Dialog()
   d.exec_()

 def doPaste(self,text):
   text = str(text)
   QApplication.clipboard().setText(text)
   msg = "Copied text: "+text[:30]
   if len(text) > 30:
     msg = msg + "... ("+str(len(text))+" chars)"
   self.iface.mainWindow().statusBar().showMessage(msg)


 def unload(self):
   # Remove the plugin
   self.iface.removePluginMenu(title,self.aMapExtent)
   self.iface.removePluginMenu(title,self.aMapExtentSQL)
   self.iface.removePluginMenu(title,self.aProjectFile)
   self.iface.removePluginMenu(title,self.aLayerInfo)
   self.iface.removePluginMenu(title,self.about)

   pass
